<template>
  <div class="h-full">
    <Forms ref="subForm" :data-from="formData" :list="searchItem" name="CustomJavaCode" :sub-loading="tableLoading" />
    <div>
      <el-button size="medium" type="primary" @click="save">新增</el-button>
      <el-button size="medium" type="primary" @click="onRefresh">刷新</el-button>
    </div>
    <div>
      <Table
        ref="main_Table"
        :btn-fix="true"
        :column-list="getTableColumn"
        :get-height="getHeight"
        :loading="tableLoading"
        name="CustomJavaCode"
        :tables="tables"
      />
    </div>
    <el-dialog v-model="showConfigModal" title="自定义JAVA代码" width="80%">
      <Forms ref="addForm" :data-from="dialogFormData" label-position="top" :list="tableColumn" name="CustomJavaCode" />
    </el-dialog>
    <el-dialog v-model="passwordModal" title="请输入密码" width="50%">
      <div class="text-center">
        <el-input v-model="passwordValue" />
        <el-button class="mt-3" size="medium" type="primary" @click="confirm">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import Forms from '@dams/components/forms'
  import Table from '@dams/components/table'
  import { getList } from '@dams/api/home.js'
  import { translateTitle } from '@/utils/i18n'
  import debounce from 'lodash/debounce'
  import { GFCD } from '@/utils/utils'
  export default {
    name: 'CustomJavaCode',
    components: { Table, Forms },
    data() {
      return {
        passwordValue: '',
        passwordModal: false,
        showConfigModal: false,
        dialogFormData: {},
        formData: {},
        clientHeight: 0,
        searchItem: {
          form: {
            name: 'search',
            field: [
              { type: 'input', label: 'Java描述', name: 'JAVA_CODE_DESCR', labelHidden: false },
              {
                type: 'button',
                label: translateTitle('Dams.ApplicationInterface.查询'),
                labelHidden: true,
              },
            ],
          },
        },
        tableLoading: false,
        tableColumn: {
          form: {
            method: '',
            name: 'dialog',
            field: [
              {
                type: 'checkbox',
                name: 'ID',
                label: '',
                hidden: true,
              },
              {
                type: 'input',
                label: 'java类全路径名称',
                name: 'JAVA_CLASS_FULL_NAME',
                isRules: true,
              },
              {
                type: 'input',
                label: 'Java描述',
                name: 'JAVA_CODE_DESCR',
                isRules: true,
              },
              {
                type: 'select',
                label: '所属子系统',
                name: 'SUB_SYSTEM',
                isRules: true,
                option: [],
              },
              {
                type: 'switch',
                label: '激活标志',
                name: 'ACTIVE_FLAG',
                isRules: true,
                activeValue: 'Y',
                inactiveValue: 'N',
              },
              {
                type: 'textarea',
                label: 'Java代码',
                name: 'JAVA_CODE',
                isRules: true,
                height: 600,
              },
              // {
              //   type: 'select',
              //   label: '所属项目',
              //   name: 'SUB_PROJECT',
              //   isRules: true,
              //   option: [],
              // },
              {
                type: 'button',
                label: '提交',
                labelHidden: true,
              },
            ],
          },
        },
        tables: {
          tableList: [],
          count: 0,
          pageSize: 150,
          limit: 1,
          name: 'CustomJavaCode',
          noAuthority: true,
          formatting: false,
        },
        configSetting: {},
        dialogType: 'add',
        deleteItem: {},
      }
    },
    computed: {
      getHeight() {
        return `${this.clientHeight - 212}`
      },
      getTableColumn() {
        const newList = []
        this.tableColumn.form.field.forEach((v) => {
          if (v.type !== 'button' && v.name !== 'JAVA_CODE') {
            newList.push({ label: v.label, porp: v.name, type: v.type })
          }
        })
        return newList
      },
    },
    created() {
      this.init()
    },
    methods: {
      async save() {
        this.dialogFormData = { ACTIVE_FLAG: 'N' }
        this.showConfigModal = true
      },
      windowResize: debounce(function () {
        this.clientHeight = document.documentElement.clientHeight
      }, 100),
      async init() {
        this.configSetting = GFCD('dams')
        this.eventInit()
        this.windowResize()
        let res = await this.getSubsystemData()
        let list = []
        res.forEach((item) => {
          list.push({ CODE: item.CODE, LABEL: item.LABEL })
        })
        this.tableColumn.form.field.forEach((item) => {
          if (item.name == 'SUB_SYSTEM') {
            item.options = list
          }
        })
      },
      async onSubmit(p) {
        let sub = p ? JSON.parse(JSON.stringify(p)) : []
        let obj = {
          '#LIMIT#': this.tables.limit,
          '#PAGE_SIZE#': this.tables.pageSize,
          ...sub[0],
        }
        this.tableLoading = true
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          data: JSON.stringify(obj),
          method: 'system.common.java.udf.code.table.query',
        })
        console.log(res)
        this.tableLoading = false
        this.tables.tableList = res.data['system.common.java.udf.code.table.query'].data
        this.tables.count = res.data['system.common.java.udf.code.table.query'].count
        this.tables.tableList.forEach((r) => {
          if (r.ACTIVE_FLAG) {
            r['ACTIVE_FLAG_table'] = r.ACTIVE_FLAG
          }
        })
      },
      //获取子系统下拉数据
      async getSubsystemData() {
        let method = 'dams.handle.flow.query.options'
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          method: method,
          data: JSON.stringify({ '#LIMIT#': 1, '#PAGE_SIZE#': 9999 }),
        })
        return res.data.SUB_SYSTEM.data
      },
      dialogFuc() {
        this.passwordModal = true
        this.passwordValue = ''
      },
      async deleteFun() {
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          data: JSON.stringify({ ID: this.deleteItem.ID, action: 'delete', instanceProcessingPassword: this.passwordValue }),
          method: 'system.common.java.udf.code.operation.service',
        })
        if (res.code == '00000') {
          this.$baseMessage('操作成功', 'success')
          this.passwordModal = false
          this.passwordValue = ''
          this.onSubmit(this.formData)
        }
      },
      onRefresh() {
        console.log(this.$refs.main_Table.$refs.xTableCustomJavaCode)
        if (this.$refs.main_Table.$refs.xTableCustomJavaCode.getCheckboxRecords().length === 0) {
          return this.$baseMessage('请选择数据', 'error')
        }

        this.dialogType = 'refresh'
        this.passwordModal = true
        this.passwordValue = ''
      },
      async refresh() {
        const ids = this.$refs.main_Table.$refs.xTableCustomJavaCode.getCheckboxRecords().map((el) => el.ID)
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          data: JSON.stringify({ ID: ids.join(','), action: 'refreshUdfJavaCode', instanceProcessingPassword: this.passwordValue }),
          method: 'system.common.java.udf.code.operation.service',
        })
        if (res.code == '00000') {
          this.$baseMessage('操作成功', 'success')
          this.passwordModal = false
          this.passwordValue = ''
          this.onSubmit(this.formData)
        }
      },
      async addOrEdit() {
        let p = {}
        p = {
          ...this.dialogFormData,
          SUB_PROJECT: this.dialogFormData.SUB_SYSTEM,
          action: this.dialogType === 'add' ? 'save' : 'update',
          instanceProcessingPassword: this.passwordValue,
        }
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          data: JSON.stringify(p),
          method: 'system.common.java.udf.code.operation.service',
        })
        if (res.code == '00000') {
          this.$baseMessage(res.msg, 'success')
          this.dialogFormData = {}
          this.passwordModal = false
          this.showConfigModal = false
          this.passwordValue = ''
          this.onSubmit(this.formData)
        }
        console.log(res)
      },
      confirm() {
        if (!this.passwordValue) {
          return this.$baseMessage('请输入密码', 'error')
        }
        if (this.dialogType === 'add' || this.dialogType === 'edit') {
          //新增或者编辑
          this.addOrEdit()
        } else if (this.dialogType === 'delete') {
          //删除
          this.deleteFun()
        } else if (this.dialogType === 'refresh') {
          //刷新
          this.refresh()
        }
      },
      async getJavaCode() {
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          data: JSON.stringify({ action: 'queryById', ID: this.dialogFormData.ID }),
          method: 'system.common.java.udf.code.operation.service',
        })
        this.dialogFormData.JAVA_CODE = res.data.JAVA_CODE
      },
      eventInit() {
        //搜索事件
        this.$sub(`CustomJavaCode_forms_on_search`, (data_arr) => {
          console.log(JSON.parse(JSON.stringify(data_arr)))
          this.formData = data_arr[0]
          this.tables.limit = 1
          this.onSubmit(data_arr)
        })
        this.$sub(`CustomJavaCode_Table_on_CustomJavaCode`, (page_arr) => {
          this.tables.limit = page_arr[0].limit
          this.tables.pageSize = page_arr[0].pageSize
          this.onSubmit(this.formData)
        })
        this.$sub(`CustomJavaCode_Table_editBtn_CustomJavaCode`, (data_arr) => {
          console.log(data_arr)
          this.dialogType = 'edit'
          this.showConfigModal = true
          this.dialogFormData = data_arr[0]
          this.getJavaCode()
        })
        this.$sub(`CustomJavaCode_Table_delBtn_CustomJavaCode`, (data_arr) => {
          console.log(data_arr)
          this.dialogType = 'delete'
          this.passwordModal = true
          this.passwordValue = ''
          this.deleteItem = data_arr[0]
        })
        // 模态框里面的确定按钮
        this.$sub(`CustomJavaCode_forms_on_dialog`, (data_arr) => {
          if (data_arr[0] == '提交') {
            // 新增模态框
            this.dialogFuc()
          }
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  :deep() {
    .el-dialog__body {
      .item_button {
        display: block !important;
        width: 100% !important;
        text-align: center;
      }
    }
  }
</style>
